﻿//
// Created by hujin on 2020/10/3.
//

#include <iostream>
#include <fstream>
#include <set>
#include "logger.h"


std::ofstream Log; // NOLINT(cert-err58-cpp)
std::ofstream Log_E; // NOLINT(cert-err58-cpp)
std::ofstream Log_E2; // NOLINT(cert-err58-cpp)


void initLog(const std::string& path) {
	Log.open(path, std::ofstream::trunc | std::ofstream::out);
}

void closeLog() {
	Log.flush();
	Log.close();
}


void initLogE(const std::string& path, const std::string& path2) {
	Log_E.open(path, std::ofstream::trunc | std::ofstream::out);
    Log_E2.open(path2, std::ofstream::trunc | std::ofstream::out);
}

void closeLogE() {
	Log_E.flush();
	Log_E.close();
    Log_E2.flush();
    Log_E2.close();
}

void log_error(int step, const std::string& error, int line, int pos) {
    const std::string step_name[] = {"Token Check", "Syntax Check", "Identifier Check", "Semantic Check", "Code optimize", "codeGen"};
	Log_E << "Line : " << line << ", \tpos : " << pos <<". \t["<<step_name[step]<< "] Error : " << error << std::endl;
}


std::set<int> logged;
void log_error2(char a, int line) {
    if(logged.count(line)) return;
    logged.insert(line);
    Log_E2 << line<<' '<<a<<std::endl;
}